package org.kotboot.ctrl

import org.kotboot.annotations.Auth
import org.kotboot.dao.AddressDao
import org.kotboot.domain.Address
import org.kotboot.enums.BoolState
import org.kotboot.module.R
import org.kotboot.service.AddressServ
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import javax.annotation.Resource


@RestController
@RequestMapping(value = "/address")
@Auth
class AddressCtrl(

        @Resource
        val addressServ : AddressServ,

        @Resource
        val addressDao : AddressDao

):BaseCtrl(){



    val LOG:Logger = LoggerFactory.getLogger(AddressCtrl::class.java)


    @RequestMapping("/{id}")
    fun delete(@PathVariable id:Long): R<*>{
        val address = Address()
        address.id = id
        address.del = BoolState.YES
        val deleteById : Int = addressDao.deleteById(address)
        if(deleteById <= 0){
            return R.ER("删除失败，请重试")
        }
        return R.ER("删除完成")
    }

    @RequestMapping(value = "/list/{userid}",method = arrayOf(RequestMethod.GET))
    fun list(@PathVariable(value = "userid") userid:Long) : R<*> {
        val map = mutableMapOf<String,Any?>()
        map.put("userid",userid)
        val active = addressServ.findByConditionActive(map)
        return R.OK(active)
    }

    @RequestMapping(value = "/" ,method = arrayOf(RequestMethod.POST))
    fun insert(address : Address) :R<*>{
        return addressServ.saveAddress(address)
    }

    @RequestMapping(value = "/" ,method = arrayOf(RequestMethod.PUT))
    fun update(address : Address) :R<*>{
        return R.OK()
    }

    @RequestMapping(value = "/setDefault/{id}",method = arrayOf(RequestMethod.PUT))
    fun setDefault(@PathVariable id:Long) :R<*>{
        return addressServ.setDefault(id)
    }






}